package com.sanjiahantang.youjiarole.module.me.quotation.preseneters;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.BaseMvpPresenter;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.me.quotation.beans.ContractRequestBean;
import com.sanjiahantang.youjiarole.module.me.quotation.beans.QuotationResponseBean;
import com.sanjiahantang.youjiarole.module.me.quotation.models.IQuotationModel;
import com.sanjiahantang.youjiarole.module.me.quotation.models.impl.QuotationModel;
import com.sanjiahantang.youjiarole.module.me.quotation.views.QuotationView;
import com.sanjiahantang.youjiarole.module.me.require.beans.RequireDetailInitResponseBean;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zy on 2017/10/31.
 */

public class QuotationPresenter extends BaseMvpPresenter<QuotationView> {

    private IQuotationModel quotationModel;
    private Dialog payDialog;
    public QuotationPresenter(){
        quotationModel = new QuotationModel();
    }

    public void loadOrder(Context context, int id, final BaseView.OnLoadListener onLoadListener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("requireId",String.valueOf(id));
        mView.showProgress();//mall/goods/phone/searchGoodsDesign
        quotationModel.searchGoodsDesign(context,param, new CallBack<QuotationResponseBean>() {
            @Override
            public void onSuccess(QuotationResponseBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
                    QuotationResponseBean.Data data = response.getData();
                    if (response.getPspId() > 0){
                        data.setPspId(response.getPspId());
                    }
                    onLoadListener.onLoad(data);
                }else{

                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    private void hideProgress() {
        if (mView != null){
            mView.hideProgress();
        }
    }


    public void doPay(final Context context, final int id, final int num, final QuotationResponseBean.Data data, final RequireDetailInitResponseBean.Data require){

        if (payDialog == null){
            payDialog = new Dialog(context, R.style.Dialog_Fullscreen);
            payDialog.setContentView(R.layout.dialog_select_pay_way);
            TextView txtNeedPrice = (TextView) payDialog.findViewById(R.id.txtNeedPrice);
            txtNeedPrice.setText(String.format("%.2f",data.getSkuPrice() * num));
            final RadioButton rbWX = (RadioButton) payDialog.findViewById(R.id.rbWX);
            final RadioButton rbZFB = (RadioButton) payDialog.findViewById(R.id.rbZFB);
            payDialog.findViewById(R.id.btnOK).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {


                    if (!rbWX.isChecked() && !rbZFB.isChecked()){
                        Utils.showToast(context,"请选择支付方式");
                    }else if(rbWX.isChecked()){

//                        saveProjectServicePriceDesignPhone(context,id,num,data,require);
                    }else{
//                        saveProjectServicePriceDesignPhone(context,id,num,data,require);
                    }
                }
            });

            payDialog.findViewById(R.id.vWX).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(true);
                    rbZFB.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vZFB).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(true);
                }
            });

            rbWX.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(true);
                    rbZFB.setChecked(false);
                }
            });

            rbZFB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(true);
                }
            });
        }

        payDialog.show();
    }

    public void saveProjectServicePriceDesignPhone(
            final Context context, ContractRequestBean data
            ) {

        if (mView == null) return;
        mView.showProgress();
        quotationModel.saveProjectServicePriceDesignPhone(context,data, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                if (mView == null) return;
                hideProgress();
                Utils.showToast(context,response.getMessage());
                if (response.getCode() == RESPONSE_CODE_SUC){
                    ((Activity)context).setResult(Activity.RESULT_OK);
                    ((Activity)context).finish();
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                hideProgress();
                Utils.showToast(context,message);
            }
        });
    }

    public void contractSignForDesign(final Context context,
                                      String customerPhone,
                                      String designPhone,
                                      String signer2,
                                      final BaseView.OnLoadListener listener) {
        Map<String,Object> param = new HashMap<>();

        param.put("otherPhone",designPhone);
        mView.showProgress();

        Map<String,Object> param2 = new HashMap<>();
        param2.put("stateId","0");
        param2.put("customerPhone",customerPhone);
        param2.put("signer2",signer2);
        param2.put("designPhone",designPhone);


        quotationModel.contractSignForDesign(context,param,param2, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                if (mView == null) return;
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
                    if (tag != null && tag instanceof HashMap){
                        listener.onLoad(tag);
                    }
                }else {
                    Utils.showToast(context,"创建合同失败");
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                hideProgress();
                Utils.showToast(context,"创建合同失败");
            }
        });
    }
}
